package com.itheima.demo.ZuoYe;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

//分析以下需求，并用代码实现
//	有100份礼品,两人同时发送，
// 当剩下的礼品小于10份的时候则不再送出，
// 利用多线程模拟该过程并将线程的名称打印出来.
public class xianc1 implements Runnable {
    int gifts = 100;
    Lock lock=new ReentrantLock();

    @Override
    public void run() {
        while (true) {
            lock.lock();
                if (gifts >0) {
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + "在送第" + gifts + "礼品");
                    gifts--;
                    if(gifts<=10){
                        System.out.println("没了");
                        break;
                    }
                }
            lock.unlock();
            }
        }
    }
